//
//  FTW.hpp
//  Directory
//
//  Created by 陈新唯 on 16/4/20.
//  Copyright © 2016年 陈新唯. All rights reserved.
//

#ifndef FTW_hpp
#define FTW_hpp


#include <string>
#include <utility>
#include <mutex>
#include "Unix.h"

struct stat;
namespace File{
    std::string path(std::string &&);
	
	class Directory;

	using numfile_t = size_t;
	struct FileTypeCounter {
		numfile_t regular = 0;
		numfile_t directory = 0;
		numfile_t block = 0;
		numfile_t character = 0;
		numfile_t fifo = 0;
		numfile_t socket = 0;
		numfile_t symlink = 0;
	};

	class FTW;
	
	using FTWError = Unix::SysError<FTW>;

	class FTW {
	public:
		FTW();
		
		bool calculate(const std::string &p);
		bool calculate(std::string &&p);
		void printAnswer();
	private:
		void files() { filesTotal = counter.regular + counter.directory + counter.block + counter.character + counter.fifo + counter.socket + counter.symlink; }
		bool calculate_imp(std::string &&);
		void check_update(const struct stat &);
		inline double cal_percent(numfile_t files) {
			return static_cast<double>(files)/filesTotal * 100;
		}
		FileTypeCounter counter = {};
		std::mutex m;
		numfile_t filesTotal = 0;
	};


};
#endif /* FTW_hpp */
